package com.ghn.cocknovel.ui.activity.address

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.ViewGroup
import chihane.jdaddressselector.AddressSelector
import chihane.jdaddressselector.OnAddressSelectedListener
import com.example.basemodel.base.BaseActivity
import com.example.basemodel.base.BaseViewModel
import com.ghn.cocknovel.BR
import com.ghn.cocknovel.R
import com.ghn.cocknovel.databinding.ActivitySelectAddressBinding
import com.kt.NetworkModel.utils.MLog

/**
 *@author: Administrator
 *2023/6/28
 *desc: 地址选择
 */
class SelecAddressActivity : BaseActivity<ActivitySelectAddressBinding, BaseViewModel>() {


    override fun initVariableId(): Int = BR.mode

    override fun initContentView(savedInstanceState: Bundle?): Int = R.layout.activity_select_address
    override fun initParam() {
        //activity 作为弹框时，要规定弹框的样式
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        val selector = AddressSelector(this)
        selector.setAddressProvider(MyAddressProvider())
        selector.onAddressSelectedListener = OnAddressSelectedListener { province, city, county, street ->
            MLog.d(province.name + "  " + city.name + "  " + county.name + "  " + street)
            // 设置结果，并进行传送
            val intent = Intent()
            intent.putExtra("provinceName", province.name)
            intent.putExtra("provinceId", province.id)
            intent.putExtra("cityName", city.name)
            intent.putExtra("cityId", city.id)
            intent.putExtra("countyName", county.name)
            intent.putExtra("countyId", county.id)
            intent.putExtra("streetName", if (street == null) "" else street.name)
            intent.putExtra("address", province.name + city.name + county.name + if (street == null) "" else street.name)
            setResult(RESULT_OK, intent)
            finish()
        }
        binding.frameLayout.addView(selector.view)
        binding.addrBack.setOnClickListener {
            finish()
        }
    }
}